﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace TBR.Common
{
    public class CookieHelper
    {
        static object Lock = new object();
        public static string GetPropertyCookieByKey(string key)
        {
            try
            {
                var cookie = GetCookiesFromIsolatedStorage().Split(new[] { ';', ',', '=' }, StringSplitOptions.RemoveEmptyEntries);
                int count = cookie.Count();
                for (int i = 0; i < count - 1; i = i + 2)
                {
                    if (cookie[i] == key)
                    {
                        return cookie[++i];
                    }
                }
                return String.Empty;
            }
            catch (Exception)
            {
                return String.Empty;
            }
        }
        public static bool SaveCookiesToIsolatedStorage(string cookieContainer)
        {
            try
            {
                lock (Lock)
                {
                    var fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
                    if (!fileStorage.DirectoryExists("Cookies"))
                    {
                        fileStorage.CreateDirectory("Cookies");
                    }
                    using (var stream = new IsolatedStorageFileStream("Cookies\\Cookies1.dat", FileMode.Create, fileStorage))
                    {
                        //BinarySerializationHelper binarySerializationHelper = new BinarySerializationHelper();
                        //binarySerializationHelper.Serialize(stream, cookieContainer);
                        var streamWriter = new StreamWriter(stream);
                        streamWriter.Write(cookieContainer);
                        streamWriter.Close();
                    }
                    return true;
                }
            }
            catch (Exception e)
            {
                return false;
            }

        }

        public static string GetCookiesFromIsolatedStorage()
        {
            try
            {
                //CookieContainer cookieContainer = null;
                string cookies = null;
                var fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
                if (fileStorage.FileExists("Cookies\\Cookies1.dat"))
                {
                    using (var stream = new IsolatedStorageFileStream("Cookies\\Cookies1.dat", FileMode.OpenOrCreate, fileStorage))
                    {
                        //BinarySerializationHelper binarySerializationHelper = new BinarySerializationHelper();
                        //cookieContainer = (CookieContainer)binarySerializationHelper.Deserialize(stream, typeof(CookieContainer));
                        var streamReader = new StreamReader(stream);
                        cookies = streamReader.ReadToEnd();
                        streamReader.Close();
                    }
                }
                return cookies;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public static CookieCollection GetCookiesFromResponse(HttpWebResponse response)
        {
            return response.Cookies;
        }
    }
}
